<!--
 * @Version: 1.0
 * @Date: 2021-06-28 14:56:46
 * @LastEditTime: 2021-07-28 16:32:11
 * @Description:
-->
<template>
  <div>
    <el-dialog :center="true" :title="title" :visible.sync="formVisibleDeliverBatch" width="700px" :before-close="() => {$emit('update:formVisibleDeliverBatch', false),$refs['ruleForm'].resetFields()}" @open="openDialog">
      <div v-loading="loading" class="from_box">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="140px" class="demo-ruleForm">
          <el-form-item label="已选定发货单">
            <div>{{ codes }}</div>
          </el-form-item>
          <el-form-item label="请选择发货方式" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio :disabled="type==='3'?true:false" label="1">自有仓库</el-radio>
              <el-radio :disabled="type==='3'?true:false" label="2">第三方仓库</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="ruleForm.type==='2'" label="选择第三方仓库" prop="warehouse">
            <el-select v-model="ruleForm.warehouse" placeholder="请选择" filterable>
              <el-option v-for="item in warehouseStockListOther" :key="item.id" :label="`${item.name}`" :value="item.dataObj" />
            </el-select>
          </el-form-item>
          <el-form-item v-if="ruleForm.type==='1'" label="选择自有仓库" prop="warehouse">
            <el-select v-model="ruleForm.warehouse" placeholder="请选择" filterable>
              <el-option v-for="item in warehouseStockList" :key="item.id" :label="`${item.name}`" :value="item.dataObj" />
            </el-select>
          </el-form-item>
          <div>
            <div style="text-align: center;margin:40px 0 20px;">
              <el-button type="primary" :loading="isbtn" @click="submitForm('ruleForm')">保存</el-button>
              <el-button @click="handleCancelDown">取消</el-button>
            </div>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getWarehouseThrid } from '@/utils/publicData'
import { getWarehouse } from '@/utils/publicData'
import { array } from 'jszip/lib/support'
export default {
  props: {
    formVisibleDeliverBatch: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    ids: {
      type: array,
      default: () => []
    },
    codes: {
      type: array,
      default: () => []
    },
    type: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      loading: false,
      isbtn: false,
      ruleForm: {
        type: '1',
        warehouse: ''
      },
      warehouseData: [],
      rules: {
        type: [
          { required: true, message: '请选择发货类型', trigger: 'blur' }
        ],
        warehouse: [
          { required: true, message: '请选择第三方仓库', trigger: 'blur' }
        ]
      },
      warehouseStockListOther: [],
      warehouseStockList: []
    }
  },
  //   mounted() {
  //     getWarehouseThrid().then((res) => {
  //       console.log('warehouseData', res)
  //       this.warehouseData = res
  //     })
  //   },
  methods: {
    openDialog() {
      console.log('======', this.type)
      this.loading = true
      getWarehouse(this.ids).then((data) => {
        this.warehouseStockList = data.map((item) => {
          item.dataObj = JSON.stringify({
            'warehouse_id': item.parent_id,
            'warehouse_fictitious_id': item.id,
            'zone': item.zone
          })
          return item
        }).filter((arr) => {
          return arr.is_third_part === 0
        })
        this.warehouseStockListOther = data.map((item) => {
          item.dataObj = JSON.stringify({
            'warehouse_id': item.parent_id,
            'warehouse_fictitious_id': item.id,
            'zone': item.zone
          })
          return item
        }).filter((arr) => {
          return arr.is_third_part === 1
        })
        this.loading = false
      })
    },
    handleCancelDown() {
      this.$emit('update:formVisibleDeliverBatch', false)
      this.$refs['ruleForm'].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isbtn = true
          const data = JSON.parse(this.ruleForm.warehouse)
          data.ids = this.ids
          data.type = this.ruleForm.type
          this.$http.post(`/deliveryOrders/batchManualShipment`, data).then(response => {
            if (response.code === 10000) {
              this.$message.success('批量发货成功')
              this.$emit('handleSearch', response.data)
              this.handleCancelDown()
              this.isbtn = false
            }
          }).catch(() => {
            this.isbtn = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
